package com.ingen.chakhleh.core.impl;

import java.util.Collection;

import com.googlecode.objectify.Key;
import com.ingen.chakhleh.core.api.dao.CourseDAO;
import com.ingen.chakhleh.core.api.dao.CurrencyDAO;
import com.ingen.chakhleh.core.api.dao.CusineDAO;
import com.ingen.chakhleh.core.api.dao.DishDAO;
import com.ingen.chakhleh.core.api.dao.DishRatingDAO;
import com.ingen.chakhleh.core.api.dao.PriceInfoDAO;
import com.ingen.chakhleh.core.api.dao.RatingTypeDAO;
import com.ingen.chakhleh.core.api.dao.RestaurantDAO;
import com.ingen.chakhleh.core.api.domain.Course;
import com.ingen.chakhleh.core.api.domain.Cusine;
import com.ingen.chakhleh.core.api.domain.Dish;
import com.ingen.chakhleh.core.api.domain.Restaurant;
import com.ingen.chakhleh.core.api.service.core.ApplicationCoreService;

public class ApplicationCoreServiceImpl implements ApplicationCoreService{

	private CourseDAO courseDAO;
	private CurrencyDAO currencyDAO;
	private CusineDAO cusineDAO;
	private DishDAO dishDAO;
	private DishRatingDAO dishRatingDAO;
	private PriceInfoDAO priceInfoDAO;
	private RatingTypeDAO ratingTypeDAO;
	private RestaurantDAO restaurantDAO;
	
	
	
	public Course getCourse(String id){
		return courseDAO.get(id);
	}
	
	public Dish getDish(String id){
		return dishDAO.get(id);
	}
	
	
	
	
    
	public Course getCourse(Key<Course> key) {
    	return courseDAO.get(key);
	}

	
	public Dish getDish(Key key) {
		return dishDAO.get(key);
	}
	
	
	
	
	
	

	
	public Collection<Course> getCourses(String propertyName, Object propertyValue) {
		return courseDAO.listByProperty(propertyName, propertyValue);
	}

	
	public Collection<Dish> getDishs(String propertyName, Object propertyValue) {
		return dishDAO.listByProperty(propertyName, propertyValue);
	}

	//For dependency injection
	public void setCourseDAO(CourseDAO courseDAO) {
		this.courseDAO = courseDAO;
	}

	public void setCurrencyDAO(CurrencyDAO currencyDAO) {
		this.currencyDAO = currencyDAO;
	}

	public void setCusineDAO(CusineDAO cusineDAO) {
		this.cusineDAO = cusineDAO;
	}

	public void setDishDAO(DishDAO dishDAO) {
		this.dishDAO = dishDAO;
	}

	public void setDishRatingDAO(DishRatingDAO dishRatingDAO) {
		this.dishRatingDAO = dishRatingDAO;
	}

	public void setPriceInfoDAO(PriceInfoDAO priceInfoDAO) {
		this.priceInfoDAO = priceInfoDAO;
	}

	public void setRatingTypeDAO(RatingTypeDAO ratingTypeDAO) {
		this.ratingTypeDAO = ratingTypeDAO;
	}

	public void setRestaurantDAO(RestaurantDAO restaurantDAO) {
		this.restaurantDAO = restaurantDAO;
	}
	
	
	
	public Collection<Restaurant> getRestaurntsServingDishWithName(String dishName){
		if(dishName==null){
			throw new IllegalArgumentException("Dish name cannot be null");
		}
		Key<Dish> dishKey = new Key<Dish>(Dish.class, dishName);
		return restaurantDAO.listByProperty("dishKeys", dishKey); 
	}
	
	public Collection<Dish> getDishesServedInRestaurant(Restaurant restaurant){
		if(restaurant == null){
			throw new IllegalArgumentException("Restaurnat cannot be null");
		}
		if(restaurant.getDishKeys()==null && restaurant.getDishKeys().isEmpty()){
			return null;
		}
		
		return dishDAO.ofy().get(restaurant.getDishKeys()).values();
	}
	
	public Collection<Restaurant> getRestaurantsWithName(String restaurantName){
		if(restaurantName == null){
			throw new IllegalArgumentException("Restaurant name cannot be null");
		}
		return restaurantDAO.listByProperty("name", restaurantName);
	}

	public Collection<Restaurant> getRestaurantsWithNamePrefixedWith(String restaurantNamePrefix){
		return restaurantDAO.getEntitiesWithNameStartingWith(restaurantNamePrefix);
	}
	
	public Collection<Dish> getDishesWithNamePrefixedWith(String dishNamePrefix){
		return dishDAO.getEntitiesWithNameStartingWith(dishNamePrefix);
	}
	
	
}
